package org.dromara.merchants.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.merchants.domain.vo.TaSetMealVo;
import org.dromara.merchants.domain.bo.TaSetMealBo;
import org.dromara.merchants.service.ITaSetMealService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 套餐
 * 前端访问路由地址为:/merchants/setMeal
 *
 * @author xiyin321
 * @date 2024-07-07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/setMeal")
public class TaSetMealController extends BaseController {

    private final ITaSetMealService taSetMealService;

    /**
     * 查询套餐列表
     */
//    @SaCheckPermission("merchants:setMeal:list")
    @GetMapping("/list")
    public TableDataInfo<TaSetMealVo> list(TaSetMealBo bo, PageQuery pageQuery) {
        return taSetMealService.queryPageList(bo, pageQuery);
    }

    /**
     * app端查询套餐列表
     * @param bo
     * @param pageQuery
     * @return
     */
    @GetMapping("/appList")
    public TableDataInfo<TaSetMealVo> appList(TaSetMealBo bo, PageQuery pageQuery) {
        return taSetMealService.queryAppList(bo, pageQuery);
    }

    /**
     * 导出套餐列表
     */
//    @SaCheckPermission("merchants:setMeal:export")
    @Log(title = "套餐", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TaSetMealBo bo, HttpServletResponse response) {
        List<TaSetMealVo> list = taSetMealService.queryList(bo);
        ExcelUtil.exportExcel(list, "套餐", TaSetMealVo.class, response);
    }

    /**
     * 获取套餐详细信息
     *
     * @param setMealId 主键
     */
    @GetMapping("/{setMealId}")
    public R<TaSetMealVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long setMealId) {
        return R.ok(taSetMealService.queryById(setMealId));
    }

    /**
     * 新增套餐
     */
//    @SaCheckPermission("merchants:setMeal:add")
    @Log(title = "套餐", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TaSetMealBo bo) {
        return toAjax(taSetMealService.insertByBo(bo));
    }

    /**
     * 修改套餐
     */
//    @SaCheckPermission("merchants:setMeal:edit")
    @Log(title = "套餐", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TaSetMealBo bo) {
        return toAjax(taSetMealService.updateByBo(bo));
    }

    /**
     * 删除套餐
     *
     * @param setMealIds 主键串
     */
//    @SaCheckPermission("merchants:setMeal:remove")
    @Log(title = "套餐", businessType = BusinessType.DELETE)
    @DeleteMapping("/{setMealIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] setMealIds) {
        return toAjax(taSetMealService.deleteWithValidByIds(List.of(setMealIds), true));
    }
}
